<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Manufacture extends CI_Controller 
{
	protected $num_lang ;
	function __construct()
	{
		parent::__construct();
		
		//load helper
	
		
			//load model

		$this->load->model('Mmanufacture');
		$this->load->model('Msetting');
		$this->num_lang = $this->Msetting->get_lang();


    //load libraries

    $this->load->library('pagination');

    $this->load->library('form_validation');
	// session
	 session_start();
		 if ( !isset($_SESSION['row_number']))

			$_SESSION['row_number']= 12;
			
	// check login
	check_login();
	}
	
	function index($page='')
	{
	
		
		$limit = $_SESSION['row_number'];

		$config['base_url'] = site_url().'/admin/manufacture/index/';

		$config['first_link'] = 'First';

		$config['last_link'] = 'Last';

		$config['next_link'] = '&gt;&gt;';

		$config['prev_link'] = '&lt;&lt;';

		$config['cur_tag_open'] = '<a href="#" class="current">';

		$config['cur_tag_close'] = '</a>';

		$config['uri_segment'] = 4;

		$config['num_links'] = 2;

		$config['total_rows'] = $this->Mmanufacture->count_all();

		$config['per_page'] = $limit;
		

		$this->pagination->initialize($config);

    if (empty($page))	$page = 0; //default value

	$data['num_lang'] =$this->num_lang;
	
    $data['result'] = $this->Mmanufacture->list_data($page,$limit);// $this->Muser->user_review($page, $limit);

    $data['title'] ='Quản lý nhà sản xuất ';//$this->lang->line('title_manufacture_review');

    $data['page'] = str_replace('&nbsp;', '', $this->pagination->create_links());

    $data['main'] = 'manufacture_review';

    $this->load->view('admin/layout/template_main', $data);
	}
	
	
	
	//***********update  *************//
	
	function update()
	{
		$id= $this->uri->segment(4);
	 if ($this->input->post('update'))

    {
	
		
		  //set rules
		if($this->num_lang >1 )
 	  		 $this->form_validation->set_rules('title_en', 'Tên nhà sản xuất - Tiếng Anh', 'required');

      $this->form_validation->set_rules('title_vi', 'Tên nhà sản xuất - Tiếng Việt', 'required');

    

    

		  if ($this->form_validation->run() && $this->sub_update())

        redirect('admin/manufacture/', 'refresh');

      else
		{
			
        	$data['missing'] = $this->form_validation->_error_array;
			
		}

    }

    elseif ($this->input->post('close'))

		{

			redirect('admin/manufacture/', 'refresh');

		}
		
			 //reload template

		$data['num_lang'] =$this->num_lang;
		$data['result']= $this->Mmanufacture->info_form_id($id);
		  $data['title'] =  $this->lang->line('title_manufacture_update');
		    $data['main'] = 'manufacture_update';
			  $this->load->view('admin/layout/template_main', $data);
		
	}
	function sub_update()
	{
		$id= $this->uri->segment(4);
		if(isset($_POST["update"]))
		{
			if(!$this->upload_file()) return false; 
		
		//print_r($_FILES);
			$status = $this->input->post('status');
			$date = time();
			
			//echo $date;
		if($_FILES['img']['error'] == 0 &&$_FILES['img']['name']  !="")
		{	
			$data = array(
			
			"ori_name" => htmlentities($this->input->post("title_vi"), ENT_QUOTES, 'UTF-8'),
			"ori_content" =>  htmlentities($this->input->post("direction_vi"), ENT_QUOTES, 'UTF-8'),
			"ori_status"=> $status,
			"ori_image"=> $_POST['img'],
			"ori_modify"=> $date,
			
			);
			$data2 = array(
			"ori_name" => htmlentities($this->input->post("title_en"), ENT_QUOTES, 'UTF-8'),
			"ori_content" =>  htmlentities($this->input->post("direction_en"), ENT_QUOTES, 'UTF-8'),
			"ori_status"=> $status,
			"ori_modify"=> $date,
			"ori_image" => $_POST['img']
			
			);
		}
		else
		{
			$data = array(
			
			"ori_name" => htmlentities($this->input->post("title_vi"), ENT_QUOTES, 'UTF-8'),
			"ori_content" =>  htmlentities($this->input->post("direction_vi"), ENT_QUOTES, 'UTF-8'),
		"ori_status"=> $status,
				"ori_modify"=> $date,
			
			);
			$data2 = array(
		"ori_status"=> $status,

			"ori_name" => htmlentities($this->input->post("title_en"), ENT_QUOTES, 'UTF-8'),
			"ori_content" =>  htmlentities($this->input->post("direction_en"), ENT_QUOTES, 'UTF-8'),
				"ori_modify"=> $date,
			
			);
		}
		if(($this->num_lang >1 ) ?$this->Mmanufacture->update($data,$id,1)&&$this->Mmanufacture->update($data2,$id,2):$this->Mmanufacture->update($data,$id,1))
			return true;
		else
			return false;
		}
		else
			return false;
		
	
	
	}
	
	function create()
	{
	 if ($this->input->post('create'))

    {
	

      //set rules
		if($this->num_lang >1 )
 	  		 $this->form_validation->set_rules('title_en', 'Tên nhà sản xuất - Tiếng Anh', 'required');

      $this->form_validation->set_rules('title_vi', 'Tên nhà sản xuất - Tiếng Việt', 'required');

    




      if ($this->form_validation->run() && $this->sub_create())

 	       redirect('admin/manufacture/', 'refresh');

      else
		{
			
        	$data['missing'] = $this->form_validation->_error_array;
			
		}

    }

    elseif ($this->input->post('close'))

		{

			redirect('admin/manufacture/', 'refresh');

		}
		$data['num_lang'] =$this->num_lang;
		$data['main'] = 'manufacture_create';

    	$data['title'] =  $this->lang->line('title_manufacture_create');

		$this->load->view('admin/layout/template_main', $data);
	}
	function sub_create()
	{
		
		if(isset($_POST["create"]))
		{
			if(!$this->upload_file())
			{
				return false;
			}
			
			$pageid = $this->Mmanufacture->get_last_id();
			//print_r($pageid);
			if(empty($pageid))
				$pid = 1;
			else
				$pid =$pageid['ori_id']+1;
			$data = array(
			"ori_id" => $pid,
			'ori_lang'=>'1',
			"ori_name" => htmlentities($this->input->post("title_vi"), ENT_QUOTES, 'UTF-8'),
			"ori_content" =>  htmlentities($this->input->post("direction_vi"), ENT_QUOTES, 'UTF-8'),
			'ori_image' =>  $this->input->post('img')
			 
			);
			$data2 = array(
			"ori_id" => $pid,
			'ori_lang'=>'2',
			"ori_name" => htmlentities($this->input->post("title_en"), ENT_QUOTES, 'UTF-8'),
			"ori_content" =>  htmlentities($this->input->post("direction_en"), ENT_QUOTES, 'UTF-8'),
			'ori_image' => $this->input->post('img')
			
			);
			
			
		if(($this->num_lang >1 )?$this->Mmanufacture->insert($data) && $this->Mmanufacture->insert($data2):$this->Mmanufacture->insert($data) )
			return true;
		else
			return false;
		}
		else
			return false;
		
	
	
	}

	
	
	
	function request()

	{

		//determine 'action'
	 

		$require = array('show', 'delete');

		foreach ($_POST as $key=>$value)

		{

			if (in_array($key, $require))

			{

				$act = $key;
				
			}

		}



		//process 'action'

		if (! empty($act))

		{

			switch ($act)

			{

				case 'show': //show

				{
				
					$_SESSION['row_number'] = $this->input->post('row_number');
					
				} break;



				case 'delete': //delete

				{

					$this->Mmanufacture->delete();

				} break;

			}//end switch



			redirect('admin/manufacture/', 'refresh');

		}//end if

	}
	
	function upload_file()

  {

    //attachment file

    $input_file = 'img';
    if ($_FILES[$input_file]['error']==0  )

    {

      $path_directory = './upload/manufacture/';

      if ( ! is_dir($path_directory)) mkdir($path_directory);

      

      $config['upload_path'] = $path_directory;

      $config['allowed_types'] = 'gif|jpg|png';

      $config['max_size'] = '500'; //500kB



      $this->load->library('upload', $config);



      //upload file

      if ($this->upload->do_upload($input_file))

      {

        $info_file = $this->upload->data();

        $file_path = $info_file['file_name'];
		

      $_POST['img'] = $file_path;

      }

      else

      {
	  	  $_POST['img'] = "";
		//$data["error"]= $this->upload->display_errors();//$data["missing"] = array("img"=>$this->upload->display_errors());
        return false;

      }

    }

    return true;

  }
		public function delete($id)
	{
		if($id >0 )
		
			$this->Mmanufacture->del_item($id);
		redirect('admin/manufacture/', 'refresh');
		
	}
	
}









?>
